#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void creat()
{
    char insert_data[100];
    char test[20];
    char table[6];
    char table_name[15];

    char rank_name[16];
    char data_type[11];
    char length[9];
    char limit[15];
    char End;
    char small_char;
    char end_kh[3];
    FILE *fp;
    char file_name[20];


    scanf("%s",table);
    if(strcmp(table,"table")!=0)
    {
        printf("input error1!");
    }
    if(strcmp(table,"table") == 0)
    {
        scanf("%s",table_name);
        getchar();
        scanf("%c",&small_char);
        do
        {
            scanf("%s",rank_name);
            scanf("%s",data_type);
            scanf("%s",length);
            //scanf("%s",limit);
            if(isWrongType(data_type))
            {
                printf("type error!\n");
                break;
            }
            if(isWrongNumber(length))
            {
                printf("length error!\n");
                break;
            }
            if(small_char != '(')
            {
                printf("input error");
                break;
            }
            strcpy(file_name,table_name);
            strcat(file_name,".txt");

            if((fp=fopen(file_name,"a+"))==NULL)
            {
                printf("File cannot be opened/n");
                exit(1);
            }

            strcpy(insert_data,rank_name);
            strcat(insert_data,"\t");
            strcat(insert_data,data_type);
            strcat(insert_data,"\t");
            strcat(insert_data,length);
            strcat(insert_data,"\n");
            fputs(insert_data,fp);
            fclose(fp);

            getchar();
            scanf("%c",&End);
            if(End == '\n')
                scanf("%s",end_kh);
            getchar();
        }
        while(End == ',');
    }
}
//
void add(char table_name[])
{
    char insert_data[100];
    char rank_name[16];
    char data_type[11];
    char file_name[20];
    FILE *fp;

    scanf("%s",rank_name);
    scanf("%s",data_type);
    if(isWrongType(data_type))
    {
        printf("type error!\n");
        return;
    }

    strcpy(file_name,table_name);
    strcat(file_name,".txt");
    if((fp=fopen(file_name,"a+"))==NULL)
    {
        printf("File cannot be opened/n");
        exit(1);
    }
    strcpy(insert_data,rank_name);
    strcat(insert_data,"\t");
    strcat(insert_data,data_type);
    strcat(insert_data,"\t");
    //strcat(insert_data,length);
    strcat(insert_data,"\n");
    fputs(insert_data,fp);

    fclose(fp);

}
void drop(char table_name[])
{
    char rank_name[16];
    char file_name[20];
    char buf[1024];
    int i = 0;
    int line_number = -1;
    int line_len[1024];
    int sum = 0;
    int x = 0;
    FILE *fp;

    scanf("%s",rank_name);
    while(rank_name[i] != '\0')
    {
        i++;
    }
    printf("i=%d\n",i);
    strcpy(file_name,table_name);
    strcat(file_name,".txt");
    if((fp=fopen(file_name,"r+"))==NULL)
    {
        printf("File cannot be opened/n");
        exit(1);
    }
    while(fgets(buf,1024,fp))
    {
        int line_length = 0;
        line_number++;
        int count = 0;
        int m;
        int n;
        while(buf[line_length] != '\0')
        {
            line_length++;
        }
        line_length++;
        line_len[line_number] = line_length;
        for(m = 0; m < i; m++)
        {
            if(buf[m] == rank_name[m])
                count++;
        }
        if(count == i)
        {
            while(x < line_number)
            {
                sum = sum + line_len[x];
                x++;
            }
            if(fseek(fp,sum,SEEK_SET) == -1)
                printf("seek error");
            for(n = 2; n < line_length; n++)
                fputc('*',fp);
            fclose(fp);
            printf("drop success!");
            return;
        }
    }
    fclose(fp);
    printf("have not this line");
    return;
}
void xdrop(char table_name[],char rank_name[])
{
    char file_name[20];
    char buf[1024];
    int i = 0;
    int line_number = -1;
    int line_len[1024];
    int sum = 0;
    int x = 0;
    FILE *fp;

    i = strlen(rank_name);
    printf("i=%d\n",i);
    strcpy(file_name,table_name);
    strcat(file_name,".txt");
    if((fp=fopen(file_name,"r+"))==NULL)
    {
        printf("File cannot be opened/n");
        exit(1);
    }
    while(fgets(buf,1024,fp))
    {
        int line_length = 0;
        line_number++;
        int count = 0;
        int m;
        int n;
        while(buf[line_length] != '\0')
        {
            line_length++;
        }
        line_length++;
        line_len[line_number] = line_length;
        for(m = 0; m < i; m++)
        {
            if(buf[m] == rank_name[m])
                count++;
        }
        if(count == i)
        {
            while(x < line_number)
            {
                sum = sum + line_len[x];
                x++;
            }
            if(fseek(fp,sum,SEEK_SET) == -1)
                printf("seek error");
            for(n = 2; n < line_length; n++)
                fputc('*',fp);
            fclose(fp);
            printf("drop success!");
            return;
        }
    }
    fclose(fp);
    printf("have not this line");
    return;
}
void modify(char table_name[])
{
    char rank_name[16];
    char file_name[20];
    char type_name[12];
    char buf[1024];
    char buf_type[12];
    char temp_line[1024];
    char temp_linetemp[1024];
    int i = 0;
    int line_number = -1;
    int line_len[1024];
    int sum = 0;
    int x = 0;
    FILE *fp;

    scanf("%s",rank_name);
    scanf("%s",type_name);
    if(isWrongType(type_name))
    {
        printf("type error!\n");
        return;
    }

    i = strlen(rank_name);
    printf("i=%d\n",i);
    strcpy(file_name,table_name);
    strcat(file_name,".txt");
    if((fp=fopen(file_name,"r+"))==NULL)
    {
        printf("File cannot be opened/n");
        exit(1);
    }
    while(fgets(buf,1024,fp))
    {
        int line_length = 0;
        line_number++;
        int count = 0;
        int m = 0;
        int n = 0;
        while(buf[line_length] != '\0')
        {
            line_length++;
        }
        line_length++;
        line_len[line_number] = line_length;
        for(m = 0; m < i; m++)
        {
            if(buf[m] == rank_name[m])
                count++;
        }
        if(count == i)
        {
            char len_buf[6];
            int sum_temp;
            char insert_data[100];

            while(x < line_number)
            {
                sum = sum + line_len[x];
                x++;
            }
            sum = sum + i + 1;

            if(fseek(fp,sum,SEEK_SET) == -1)
                printf("seek error");
            fscanf(fp,"%s",buf_type);
            sum_temp = sum+strlen(buf_type);

            if(fseek(fp,sum_temp,SEEK_SET) == -1)
                printf("seek error");
            fscanf(fp,"%s",len_buf);
            printf("len_buf = %s\n",len_buf);

            strcpy(insert_data,rank_name);
            strcat(insert_data,"\t");
            strcat(insert_data,type_name);
            strcat(insert_data,"\t");
            strcat(insert_data,len_buf);
            strcat(insert_data,"\n");
            fseek(fp,0,SEEK_END);
            fputs(insert_data,fp);
            fclose(fp);
            xdrop(table_name,rank_name);

            printf("modify success!");
            return;
        }
    }
    fclose(fp);
    printf("have not this line");
    return;
}
void show(char table_name[])
{
    FILE *fp;
    char file_name[20];
    char buf[1024];
    strcpy(file_name,table_name);
    strcat(file_name,".txt");
    if((fp=fopen(file_name,"r+"))==NULL)
    {
        printf("File cannot be opened\n");
        exit(1);
    }
    printf("|rank|\t|type|\t|length|\n");
    printf("---------------------------\n");
    while(fgets(buf,1024,fp))
    {
        if(buf[0]!='*')
            printf("%s\n",buf);
    }
    printf("---------------------------\n");
    fclose(fp);
}
int isWrongType(char type_name[])
{
    if(strcmp(type_name,"int") == 0)
        return 0;
    else if(strcmp(type_name,"char") == 0)
        return 0;
    else if(strcmp(type_name,"double") == 0)
        return 0;
    else if(strcmp(type_name,"varchar") == 0)
        return 0;
    else if(strcmp(type_name,"decimal") == 0)
        return 0;
    else
        return 1;

}
int isWrongNumber(char len_name[])
{
//    int i=0;
//    while(len_name[i] != '\0')
//    {
//        i++;
        if(len_name[0]>='0'&&len_name[0]<='9')
            return 0;
        else
            return 1;
//    }
}
int main()
{
    printf("---------------------Sample Input----------------------------\n\n");
    printf("Create: \ncreate table sql (\nrank_name1 type1 length1 ,\nrank_name2 type2 length2\n);\n");
    printf("\nAdd:\nalter table table_name add new_rank_name new_type_name\n");
    printf("\nDrop:\nalter table table_name drop rank_name\n");
    printf("\nModify:\nalter table table_name modify rank_name new_type_name\n\n");
    printf("--------------------------------------------------------------\n\n");

    int login = 0;
    char code[6];
    char ctemp_edit[15][15];
    char table[6];
    char table_name[15];
    char operation[8];
    char judge[8];


    printf("\n------------Please Login--------------");
    int i;
    for(i=0; i<5; i++)
    {
        printf("\nLOG>Password:");
        scanf("%s",code);
        if(strcmp(code,"admin")==0)
        {
            printf("\nWelcome!\n");
            login=1;
            break;
        }
        else
        {
            printf("\nLog>Login failed!\n");
            login=0;
        }
    }

    while(1)
    {
        printf("\nSql>");
        scanf("%s",judge);
        if(strcmp(judge,"show") == 0)
        {
            scanf("%s",table_name);
            show(table_name);
        }
        else if(strcmp(judge,"create") == 0)
        {
            creat();
        }
        else if(strcmp(judge,"alter") == 0)
        {
            scanf("%s",table);
            if(strcmp(table,"table") == 0)
            {
                scanf("%s",table_name);
                scanf("%s",operation);
                if( (strcmp(operation,"add")==0) | (strcmp(operation,"ADD")==0) )
                    add(table_name);
                else if( (strcmp(operation,"drop")==0) | (strcmp(operation,"DROP")==0) )
                    drop(table_name);
                else if( (strcmp(operation,"modify")==0) | (strcmp(operation,"MODIFY")==0) )
                    modify(table_name);
                else
                    printf("error");

            }
        }
    }

}